import htmlToWord.CustomXWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.List;

/**
 * 描述:
 *
 * @author yanzhengwu
 * @create 2021-01-14 16:27
 */
public class Main {

    public static void main(String[] args) throws IOException {

        CustomXWPFDocument xwpfDocument = new CustomXWPFDocument();
        XWPFTable table = xwpfDocument.createTable();
        XWPFTableRow row1 = table.getRow(0);
        XWPFTableCell cell0 = row1.getCell(0);
        XWPFTableCell cell1 = row1.createCell();
        XWPFTableCell cell2 = row1.createCell();
        setCellWith(cell0, 1000);
        setCellWith(cell1, 1000);
        setCellWith(cell2, 1000);

        XWPFTableRow row2 = table.createRow();
        XWPFTableCell cell00 = row2.getCell(0);
        XWPFTableCell cell11 = row2.getCell(1);
        XWPFTableCell cell22 = row2.getCell(2);
        setCellWith(cell00, 1000);
        setCellWith(cell11, 1000);
        setCellWith(cell22, 1000);


        XWPFTableCell startCell = row2.getCell(0);
        XWPFTableCell endCell = row2.getCell(1);
        XWPFTableCell endCell2 = row2.getCell(2);

        List<XWPFTableCell> cells = row2.getTableCells();
        for (int i = 0; i < cells.size(); i++) {
            XWPFTableCell cell = cells.get(i);
            if (i == 0) {
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
            } else {
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
            }
        }


       /* startCell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);

        endCell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);

        endCell2.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);*/

//        XWPFTableCell startCell2 = row2.getCell(1);
//        startCell2.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
//        XWPFTableCell endCell2 = row2.getCell(2);
//        endCell2.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);

        /*XWPFTableCell startCell2 = row2.getCell(1);
        XWPFTableCell endCell2 = row2.getCell(2);


        startCell2.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);

        endCell2.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);*/


        Long date = System.currentTimeMillis();
        String path = "F:/htmlToWord/" + date + "text.docx";
        OutputStream outputStream = new FileOutputStream(path);
        System.out.println("导出完成:" + path);
        xwpfDocument.write(outputStream);
        outputStream.close();

    }

    public static void addStyle(XWPFTableRow row) {
        CTTrPr trPr = row.getCtRow().addNewTrPr();
        trPr.addNewGridAfter();
        trPr.addNewGridBefore();
        CTHeight h = trPr.addNewTrHeight();
        h.setVal(BigInteger.valueOf(2000));

    }

    private static void setCellWith(XWPFTableCell cell, Integer width) {
        //设置列宽
        CTTcPr cPr1 = cell.getCTTc().addNewTcPr();
        CTTblWidth w = cPr1.addNewTcW();
        w.setW(BigInteger.valueOf(width));
        CTTcPr ctTcPr = cell.getCTTc().getTcPr();
        //System.out.println(ctTcPr);
    }

    // word跨列合并单元格
    public void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
        for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
            XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
            if (cellIndex == fromCell) {
                // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
            } else {
                // Cells which join (merge) the first one, are set with CONTINUE
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
            }
        }
    }

}
